package Server;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Despachante {
	
	public Despachante(){
	}
	
	public String invoke(String message){
		String[] m = message.split(","); 
		try {
			String classe = "Server."+m[0]+"Esqueleto";
			Class<?> c = Class.forName(classe);
			Object obj = c.newInstance();
			Method met = c.getMethod(m[1], String.class);
			String result = null;
			if (m[1].equals("adicionar"))
				result = (String) met.invoke(obj, m[2]+","+m[3]+","+m[4]+","+m[5]); 
			else if (m[1].equals("logar"))
				result = (String) met.invoke(obj, m[2]+","+m[3]);
			return result;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
